﻿using CMS.Models;
using CMS.Windows;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.ViewModel
{
    public class RecordViewModel : ViewModelBase
    {

        public RecordViewModel()
        {

        }

        private List<Record> record = new List<Record>();
        /// <summary>
        /// 所有物资
        /// </summary>
        public List<Record> Records
        {
            get
            {
                return record;
            }
            set
            {
                this.record = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 打开入库对话框
        /// </summary>
        public RelayCommand<Record> OpenInputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Record>((record) =>
                {
                    var inputCargoViewModel = ServiceLocator.Current.GetInstance<InputCargoViewModel>();
                    if (inputCargoViewModel == null) return;
                    var cargo = new CargoProvider().Select().FirstOrDefault(item => item.Id == record.CargoId);
                    if (cargo == null) return;
                    inputCargoViewModel.Cargo = cargo;
                    var window = new InputCargoWindow();
                    object value = window.ShowDialog();
                    // 刷新数据
                    Records = new RecordProvider().Select();
                });
            }
        }

        /// <summary>
        /// 打开出库对话框
        /// </summary>
        public RelayCommand<Record> OpenOutputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Record>((record) =>
                {
                    var outputCargoWindow = ServiceLocator.Current.GetInstance<OutputCargoViewModel>();
                    if (outputCargoWindow == null) return;
                    var cargo = new CargoProvider().Select().FirstOrDefault(item => item.Id == record.CargoId);
                    if (cargo == null) return;
                    outputCargoWindow.Cargo = cargo;
                    var window = new OutputCargoWindow();
                    object value = window.ShowDialog();
                    // 刷新数据
                    Records = new RecordProvider().Select();
                });
            }
        }
    }
}
